package cn.liyunqi.ttv.start.action;

import cn.liyunqi.core.exception.BizException;
import cn.liyunqi.ttv.start.localdb.ConfigHelper;
import cn.liyunqi.ttv.start.util.HttpUtil;
import cn.liyunqi.ttv.start.view.VersionInfo;
import com.alibaba.fastjson.JSONObject;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TTVActionUtil {

    // 从服务器获得一个输入流(本例是指从服务器获得一个image输入流)
    public static InputStream getInputStream(String urlPath) {
        InputStream inputStream = null;
        HttpURLConnection httpURLConnection = null;

        try {
            URL url = new URL(urlPath);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            // 设置网络连接超时时间
            httpURLConnection.setConnectTimeout(3000);
            // 设置应用程序要从网络连接读取数据
            httpURLConnection.setDoInput(true);

            httpURLConnection.setRequestMethod("GET");
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode == 200) {
                // 从服务器返回一个输入流
                inputStream = httpURLConnection.getInputStream();

            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return inputStream;

    }

    //把从服务器获得图片的输入流InputStream写到本地磁盘
    public static void getResources(String url,String file) {

        InputStream inputStream = getInputStream(url);
        byte[] data = new byte[1024];
        int len = 0;
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            while ((len = inputStream.read(data)) != -1) {
                fileOutputStream.write(data, 0, len);

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

    }


    public static boolean sendMailCode(String mail,Integer type){
        try {
            String url = ConfigHelper.getloadValue("url");

            Map<String,String> param = new HashMap<>();
            param.put("mail",mail);
            param.put("type",type + "");
            String result = HttpUtil.sendPost(url + "/user/getMail/register",param);

            JSONObject rejson = JSONObject.parseObject(result);

            if("0000".equals(rejson.get("STATUS"))){
                return true;
            }else{
                throw new BizException(rejson.get("MESSAGE").toString());
            }

        }  catch (BizException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("邮箱验证码发送失败");
        }
    }

    public static boolean isEmail(String string) {
        if (string == null)
            return false;
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(string);
        if (m.matches())
            return true;
        else
            return false;
    }

    public static boolean register(String mail, String mailCode, String password, String recommendMail) {
        try {
            String url = ConfigHelper.getloadValue("url");

            Map<String,String> param = new HashMap<>();
            param.put("mail",mail);
            param.put("mailCode",mailCode);
            param.put("password",password);
            param.put("recommendMail",recommendMail);
            String result = HttpUtil.sendPost(url + "/user/register",param);

            JSONObject rejson = JSONObject.parseObject(result);

            if("0000".equals(rejson.get("STATUS"))){
                return true;
            }else{
                throw new BizException(rejson.get("MESSAGE").toString());
            }

        }  catch (BizException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("注册失败");
        }
    }

    public static JSONObject login(String mail, String password) {
        try {
            String url = ConfigHelper.getloadValue("url");

            Map<String,String> param = new HashMap<>();
            param.put("mail",mail);
            param.put("password",password);
            String result = HttpUtil.sendPost(url + "/user/login",param);

            JSONObject rejson = JSONObject.parseObject(result);

            if("0000".equals(rejson.get("STATUS"))){
                return rejson.getJSONObject("vo");
            }else{
                throw new BizException(rejson.get("MESSAGE").toString());
            }

        }  catch (BizException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("邮箱验证码发送失败");
        }
    }
    public static VersionInfo getNewVersion(){
        try {
            String url = ConfigHelper.getloadValue("url");
            String result  =HttpUtil.sendGet(url + "/common/getVersion",null);

            JSONObject rejson = JSONObject.parseObject(result);
            if("0000".equals(rejson.get("STATUS"))){
                return rejson.getObject("vo",VersionInfo.class);
            }else{
                throw new BizException(rejson.get("MESSAGE").toString());
            }
        } catch (Exception e) {
            throw new BizException("最新版本号获取异常");
        }
    }

    public static boolean resetPassword(String mail, String mailCode, String password) {
        try {
            String url = ConfigHelper.getloadValue("url");

            Map<String,String> param = new HashMap<>();
            param.put("mail",mail);
            param.put("mailCode",mailCode);
            param.put("password",password);
            String result = HttpUtil.sendPost(url + "/user/resetPassword",param);

            JSONObject rejson = JSONObject.parseObject(result);

            if("0000".equals(rejson.get("STATUS"))){
                return true;
            }else{
                throw new BizException(rejson.get("MESSAGE").toString());
            }

        }  catch (BizException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("修改失败");
        }
    }
}
